package cn.restcloud.example.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.bson.Document;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.rpc.service.GenericService;
import cn.restcloud.framework.core.annotation.bean.BeanConfig;
import cn.restcloud.framework.core.annotation.bean.BeanType;
import cn.restcloud.framework.core.annotation.rest.MethodType;
import cn.restcloud.framework.core.annotation.rest.Param;
import cn.restcloud.framework.core.annotation.rest.Params;
import cn.restcloud.framework.core.annotation.rest.RestConfig;
import cn.restcloud.framework.view.base.DocAndView;
import cn.restcloud.framework.view.util.ViewUtil;

@BeanConfig(appId = "example",beanId = "DubboClientRest",beanName = "Dubbo服务调用示例",beanType = BeanType.CONTROLLER,autowired = true,singleton = true,hotMode = true)
@RestConfig(url = "/rest/example/dubbo")
public class DubboClientRest {

	@RestConfig(configName = "Hello测试",method = MethodType.GET,url = "/hello")
	@Params({ 
		@Param(id = "param", name = "唯一id",required=true,defaultValue="test hello"),
	})
	public DocAndView hello(String param) throws Exception {
		
		ApplicationConfig application = new ApplicationConfig();
		application.setName("restcloud-dubbo-provider");

		RegistryConfig registry = new RegistryConfig();
		registry.setAddress("zookeeper://120.77.47.73:2181");

		application.setRegistry(registry);

		ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>();
		
		// 弱类型接口名
		reference.setInterface("cn.restcloud.springboot.IDubboService");
		
		// 声明为泛化接口
		reference.setGeneric(true);

		reference.setApplication(application);

		// 用com.alibaba.dubbo.rpc.service.GenericService可以替代所有接口引用
		GenericService genericService = reference.get();

		String name = (String) genericService.$invoke("hello", new String[]{String.class.getName()}, new Object[]{param});
		
		return ViewUtil.getJsonView(name);
	}

	@RestConfig(configName = "获取用户对像",method = MethodType.GET,url = "/getUserInfo")
	@Params({ 
		@Param(id = "userId", name = "userid",required=true),
		@Param(id = "userName", name = "userName",required=true),
	})
	public DocAndView getUserInfo(String userId,String userName) throws Exception {
		
		ApplicationConfig application = new ApplicationConfig();
		application.setName("restcloud-dubbo-provider");

		RegistryConfig registry = new RegistryConfig();
		registry.setAddress("zookeeper://120.77.47.73:2181");

		application.setRegistry(registry);

		ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>();
		
		// 弱类型接口名
		reference.setInterface("cn.restcloud.springboot.IDubboService");
		
		// 声明为泛化接口
		reference.setGeneric(true);

		reference.setApplication(application);

		// 用com.alibaba.dubbo.rpc.service.GenericService可以替代所有接口引用
		GenericService genericService = reference.get();

		String[] parameterTypes = new String[]{"cn.restcloud.springboot.UserObj"};
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("class", "cn.restcloud.springboot.UserObj");
		params.put("userId", userId);
		params.put("userName", userName);
		
		Object userInfo =genericService.$invoke("getUserInfo", parameterTypes, new Object[]{params});
		
		return ViewUtil.getJsonView(userInfo);
	}
	
	@RestConfig(configName = "获取用户对像",method = MethodType.GET,url = "/getUserInfo/params")
	@Params({ 
		@Param(id = "userId", name = "userid",required=true),
		@Param(id = "userName", name = "userName",required=true),
	})
	public DocAndView getUserInfoAndParams(String userId,String userName) throws Exception {
		
		ApplicationConfig application = new ApplicationConfig();
		application.setName("restcloud-dubbo-provider");

		RegistryConfig registry = new RegistryConfig();
		registry.setAddress("zookeeper://120.77.47.73:2181");

		application.setRegistry(registry);

		ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>();
		
		// 弱类型接口名
		reference.setInterface("cn.restcloud.springboot.IDubboService");
		
		// 声明为泛化接口
		reference.setGeneric(true);
		reference.setTimeout(30000);
		reference.setApplication(application);

		// 用com.alibaba.dubbo.rpc.service.GenericService可以替代所有接口引用
		GenericService genericService = reference.get();
	
		//准备参数类型
		List<Object> methodParamTypes=new ArrayList<Object>();
		methodParamTypes.add("cn.restcloud.springboot.UserObj");
		methodParamTypes.add(int.class.getName());
		String[] parameterTypes = methodParamTypes.toArray(new String[methodParamTypes.size()]);
		
		//准备参数值
		List<Object> methodParamValues=new ArrayList<Object>();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", userId);
		params.put("userName", userName);
		methodParamValues.add(params);
		methodParamValues.add(102);
		
		
		Object[] paramsValues=methodParamValues.toArray(new Object[methodParamValues.size()]);
		
//		PrintUtil.o(int.class.getName());
//		PrintUtil.o(String.class.getName());
//		PrintUtil.o(long.class.getName());
//		PrintUtil.o(float.class.getName());
//		PrintUtil.o(double.class.getName());
//		PrintUtil.o(List.class.getName());
		
		Map<String,Object> objmap =(Map<String,Object>)genericService.$invoke("getUserInfo2", parameterTypes, paramsValues);
		Document indoc=new Document();
		indoc.putAll(objmap);
		return ViewUtil.getJsonView(indoc);
	}
	
}
